import os
import time

import pandas as pd


def write_to_excel():
    try:
        data = {
            "姓名": ["张三", "李四", "王五"],
            "年龄": [26, 30, 45],
            "城市": ["北京", "上海", "深圳"]
        }
        df = pd.DataFrame(data)
        file_path = ".test.xlsx"
        if os.path.exists(file_path):
            os.remove(file_path)
            print("删除已存在文件")
    except Exception as e:
        print("数据格式错误")

    try:
        df.to_excel(".test.xlsx")
        print("写入成功")
    except Exception as e:
        print("写入失败")


def read_from_excel():
    try:
        df = pd.read_excel(".test.xlsx")
        print("将读取到的数据转换为列表")
        data_list = df.values.tolist()
        print(data_list)
        print("读取成功")
        return data_list
    except Exception as e:
        print("读取失败")
        return None


def read_new_excel():
    try:
        df = pd.read_excel("pythonclassexcel.xlsx")
        print("将读取到的数据转换为列表")
        data_list = df.values.tolist()
        print(data_list)
        print("读取成功")
        return data_list
    except Exception as e:
        print("读取失败")
        return None


if __name__ == '__main__':
    print("Hello Python")
    # write_to_excel()
    # time.sleep(5)
    # data = read_from_excel()
    #
    # databayss = []
    # datakeys = ["id", "姓名", "年龄", "地址"]
    #
    # if data is None:
    #     print("没有数据")
    # else:
    #     print("有数据")
    #     print("遍历列表")
    #     for i in range(len(data)):
    #         databayss.append(dict(zip(datakeys, data[i])))
    #     print(databayss)

    print(read_new_excel())